package world.criver.injectmoduleinfo.conteller;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
import world.criver.injectmoduleinfo.function.funFile;
import world.criver.injectmoduleinfo.function.funInject;
import world.criver.injectmoduleinfo.modular.moResult;

import java.io.File;
import java.time.LocalDate;
import java.time.LocalDateTime;

import static java.lang.StringTemplate.STR;

public class conMaster {

    public TextField jar_url;
    public TextArea log;
    public Button button_inject;
    public Button button_cancel;
    public Button button_edit;
    public Button button_select;
    private funFile funFile=new funFile();
    private funInject funInject=new funInject();
    private File dir=null;//文件选择器打开位置
    private File target=null;//选中的jar
    private int active_num=0;//注入的进度
    private boolean activeing=false;//注入中？
    public void onSelect(ActionEvent actionEvent) {
        FileChooser fc=new FileChooser();
        fc.setTitle("选择jar文件");
        fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("JAR","*.jar"));
        if(dir!=null)fc.setInitialDirectory(dir);
        File f=fc.showOpenDialog(null);
        if(f!=null){
            target=f;
            dir= new File(f.getPath());
            jar_url.setText(f.getPath());
            if(active_num>0)addLog("[系统]取消注入。");
            active_num=0;
            activeing=false;
        }
    }
    public void onInject(ActionEvent actionEvent) {
        start();
        LocalDateTime d=LocalDateTime.now();
        moResult mo;
        if(active_num==0) {
            addLog(STR. "[系统]>>>\{ d.getYear() }-\{ d.getMonth().getValue() }-\{ d.getDayOfMonth() } \{ d.getHour() }:\{ d.getMinute() }:\{ d.getSecond() }" );
            if (target == null) {
                addLog("[系统]请选择需要注入的文件!");
                stop();
                return;
            }
            active_num = 1;//验证jar成功
        }
        if(active_num==1) {
            addLog("[系统]开始备份...");
            if (!funFile.backups(target.getAbsolutePath())) {
                addLog("[系统]备份失败！");
                stop();
                return;
            }
            active_num = 2;//备份成功
        }
        if(active_num==2) {
            addLog("[系统]备份完成。\n[系统]生成 module-info.java ...");
            mo = funInject.create_modular_info(target.getAbsolutePath());
            if (!mo.isState()) {
                addLog(STR. "[系统]生成 module-info.java 失败！\n\{ mo.getContent() }" );
                stop();
                return;
            }
            active_num = 3;//生成 module-info.java 成功
        }
        if(active_num==3) {
            addLog("[系统]生成 module-info.java 成功！\n[系统]编译 module-info.class ...");
            mo = funInject.compile_module_info(target.getAbsolutePath());
            if (!mo.isState()) {
                addLog(STR. "[系统]编译 module-info.class 失败！\n\{ mo.getContent() }" );
                stop();
                return;
            }
            active_num = 4;//编译 module-info.java 成功
        }
        if(active_num==4) {
            addLog("[系统]编译 module-info.class 成功。\n[系统]开始注入...");
            mo = funInject.inject_module_info(target.getAbsolutePath());
            if (!mo.isState()) {
                addLog(STR. "[系统]注入失败！\n\{ mo.getContent() }" );
                stop();
                return;
            }
            active_num = 0;
            addLog("[系统]注入成功！\n[系统]结束注入。");
        }
        stop();
    }

    public void onEdit(ActionEvent actionEvent) {
        funInject.openFolder();
    }

    public void onCancel(ActionEvent actionEvent) {
        active_num=0;
        addLog("[系统]取消注入。");
        activeing=false;
    }
    private void addLog(String message){
        Platform.runLater(()->{
            log.appendText(STR."\{message}\n");
        });
    }
    private void start(){
        activeing=true;
        updateButton();
    }
    private void stop(){
        activeing=false;
        updateButton();
    }
    private void updateButton(){
        button_select.setDisable(activeing);
        button_inject.setDisable(activeing);
        button_edit.setDisable(!activeing && active_num==3);
        button_cancel.setDisable(!activeing && active_num>0);
    }
}